﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using LitJson;

[AddComponentMenu("PUEX/Affector/Velocity/JetSpeedOverLifetime")]
public class PUEXJetSpeedOverLifetimeAffector : PUEXAffector {
	
	#region - Editable property
	public PUEXDynamicAttribute jetRate;
	public bool useEmitterTime;
	#endregion
	
	#region - Lifecycle
	public PUEXJetSpeedOverLifetimeAffector ()
	{
		jetRate = PUEXDynamicAttribute.GenerateFixed(1f);
		useEmitterTime = false;
	}
	#endregion
	
	#region - Update
	public override void update (float dt, PUEXParticle particle)
	{
		float t = particle.timeFraction;
		if (useEmitterTime)
			t = inTrigger () ? triggerTimeFraction : ownerEmitter.timeFraction;

		particle.speed = particle.originalSpeed * jetRate.getValue (t);
	}
	#endregion

	#region - Param property
	public override void updateParams (PUEXParticleSystem system)
	{
		jetRate.parameterize (system);
	}
	#endregion

	#region - Serialization
	public override JsonData save (JsonData emitterData, JsonData systemData)
	{
		JsonData ret = base.save (emitterData, systemData);
		
		ret ["jet_rate"] = PUEXUtils.serializeDynamicAttribute (jetRate);
		ret ["use_emitter_time"] = useEmitterTime;
		
		return ret;
	}
	#endregion
	
}
